package gui;

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.ListCellRenderer;


public class ColorListCellRenderer implements ListCellRenderer<Color> {
	private static final ListCellRenderer<Object> renderer = new DefaultListCellRenderer();
	
	public static Color[] colorTab = {Color.GREEN, Color.CYAN, Color.BLUE,  Color.MAGENTA, Color.PINK, Color.RED, Color.ORANGE, Color.YELLOW, Color.WHITE, Color.LIGHT_GRAY, Color.GRAY, Color.DARK_GRAY, Color.BLACK};
	
	private static Color getContrastColor(Color color) {
	  double y = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue()) / 1000;
	  return y >= 128 ? Color.black : Color.white;
	}
	
	@Override
	public Component getListCellRendererComponent(JList<? extends Color> list,
			Color value, int index, boolean isSelected, boolean cellHasFocus) {
		if(value == null) {
			return renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		}
		
		//System.out.println("" + value + " : "+index+" "+isSelected+" "+cellHasFocus);
		
		Component comp = renderer.getListCellRendererComponent(list, Integer.toHexString(value.getRGB()), index, isSelected, cellHasFocus);
		
        if (isSelected) {
        	comp.setBackground(getContrastColor(value));
        	comp.setForeground(value);
        } else {
        	comp.setBackground(value);
        	comp.setForeground(getContrastColor(value));
        }
		return comp;
	}
}
